/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vnexpress;

import DTO.TinTucDTO;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Mr_Quay
 */
public class Phapluat {

    /**
     * @param args the command line arguments
     */
    // "//*[@id=\"BodyWraper\"]/div/div[1]/div[4]/div[1]/div[2]/div[4]/div/h2/a"
    public String getTitle(String xmlFile, String xPathTitle) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);   
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        XPathExpression xp = xPath.compile(xPathTitle);

        // <a>in cai nay </a>
        Object kq = xp.evaluate(doc, XPathConstants.NODE);
        Node nodes = (Node) kq;
        return nodes.getTextContent();

    }

    public String getLink(String xmlFile, String xPathLink) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);
        //m thử copy xpath thẻ <div> coi   
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        XPathExpression xp = xPath.compile(xPathLink);
        //xPath.setNamespaceContext(new MineralOccurrenceNamespaceContext());

        // <a href="http://abc.com.vn" name="aadad"></a>         
        Object kq = xp.evaluate(doc, XPathConstants.NODE);
        Element nodes = (Element) kq;
        return nodes.getAttribute("href");
    }

    public String getDate(String xmlFile, String xPathDate) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);
        //m thử copy xpath thẻ <div> coi   
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        XPathExpression xp = xPath.compile(xPathDate);

        // <a>in cai nay </a>
        Object kq = xp.evaluate(doc, XPathConstants.NODE);
        Node nodes = (Node) kq;
        return nodes.getTextContent();

    }

    public String getImage(String xmlFile, String xPathImage) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);
        //m thử copy xpath thẻ <div> coi   
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        XPathExpression xp = xPath.compile(xPathImage);
        //xPath.setNamespaceContext(new MineralOccurrenceNamespaceContext());

        // <a href="http://abc.com.vn" name="aadad"></a>         
        Object kq = xp.evaluate(doc, XPathConstants.NODE);
        Element nodes = (Element) kq;
        return nodes.getAttribute("src");
    }

    public String getTomtat(String xmlFile, String xPathTomtat) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);
        //m thử copy xpath thẻ <div> coi   
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        XPathExpression xp = xPath.compile(xPathTomtat);

        // <a>in cai nay </a>
        Object kq = xp.evaluate(doc, XPathConstants.NODE);
        Node nodes = (Node) kq;
        return nodes.getTextContent();

    }

    public int Count(String xmlFile, String xPathCha) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(xmlFile);
        //m thử copy xpath thẻ <div> coi   
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        XPathExpression xp = xPath.compile(xPathCha);

        Object kq = xp.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) kq;
        return nodes.getLength();

    }
//    public List<DTO.TinTucDTO> LoadDoisong() throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
//        List<DTO.TinTucDTO> lst=new ArrayList<>();
//        Phapluat ds=new Phapluat();
//        int n = ds.Count("exphapluat.xml", "//*[@id=\"content\"]/div[1]/div[2]/*");
//
//        for(int i=1;i<=15;i++){
//            DTO.TinTucDTO dto=new TinTucDTO("http://vnexpress.net/tin-tuc/phap-luat");
//            String xpathTitle = "//*[@id=\"content\"]/div[1]/div[2]/div[" + i + "]/div/h2/a";
//            String title=ds.getTitle("exphapluat.xml", xpathTitle);
//            dto.setTieude(title);
//            
//            String xpathLink = "//*[@id=\"content\"]/div[1]/div[2]/div[" + i + "]/div/h2/a";
//            String link = ds.getLink("exphapluat.xml", xpathLink);
//            dto.setLink(link);
//
//            String xpathDate = "//*[@id=\"content\"]/div[1]/div[2]/div[" + i + "]/div/p";
//            String date = ds.getDate("exphapluat.xml", xpathDate);
//            dto.setNgaydang(date);
//
//            String xpathImgage="//*[@id=\"content\"]/div[1]/div[2]/div["+i+"]/div/a/img";
//            String image=ds.getImage("exphapluat.xml", xpathImgage);
//            dto.setHinhanh(image);
//
//            String xpathTomtat = "//*[@id=\"content\"]/div[1]/div[2]/div[" + i + "]/div/h3";
//            String tomtat = ds.getTomtat("exphapluat.xml", xpathTomtat);
//            dto.setTomtat(tomtat);
//            lst.add(dto);
//        }
//        return lst;
//        
//   }
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {      
        Phapluat bd=new Phapluat();
        int n = bd.Count("exphapluat.xml", "//*[@id=\"content\"]/div[1]/div[2]/*");
        System.out.println(n+"");
        for(int i=1;i<=15;i++){
            String xpathTitle = "//*[@id=\"content\"]/div[1]/div[2]/div[" + i + "]/div/h2/a";
            String title=bd.getTitle("exphapluat.xml", xpathTitle);
            System.out.println(i+"----Title:"+title);
            
            String xpathLink = "//*[@id=\"content\"]/div[1]/div[2]/div[" + i + "]/div/h2/a";
            String link = bd.getLink("exphapluat.xml", xpathLink);
            System.out.println("Link:" + link);

            String xpathDate = "//*[@id=\"content\"]/div[1]/div[2]/div[" + i + "]/div/p";
            String date = bd.getDate("exphapluat.xml", xpathDate);
            System.out.println("----Date:" + date);

            String xpathImgage="//*[@id=\"content\"]/div[1]/div[2]/div["+i+"]/div/a/img";
            String image=bd.getImage("exphapluat.xml", xpathImgage);
            System.out.println("----Image:"+image);

            String xpathTomtat = "//*[@id=\"content\"]/div[1]/div[2]/div[" + i + "]/div/h3";
            String tomtat = bd.getTomtat("exphapluat.xml", xpathTomtat);
            System.out.println("----Tomtat:" + tomtat);
        }

    }
}
